/**
 * All rights Reserved
 *
 * @Title: VideoTableHandler.java
 * @Package com.amigo.online.provider.sync.abs
 * @Description: (用一句话描述该文件做什么)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月15日 下午3:56:35
 * @version V1.0
 * @Copyright: 2018  All rights reserved.
 */
package com.amigo.online.provider.sync.abs;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述） 
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月15日 下午3:56:35  
 *
 */

@Component
public class CommonTableHandler implements TableHandler {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    SyncDataHandleCenter syncDataHandleCenter;

    private static final String EXECUTOR_BEAN_SUFFIX = "QueueThreadPool";

    @Override
    public void updateHandler(JSONObject newData, Set<String> updateSet, String tableName) {
        Map<String, List<String>> map = syncDataHandleCenter.getTableTargetSource();
        if (map != null) {
            List<String> sourceList = map.get(tableName);
            if (sourceList != null) {
                for (String temp : sourceList) {
                    ColumnHandleMap hadlerMap = (ColumnHandleMap) applicationContext.getBean(temp + tableName);
                    DataHandlerService dataHandlerService = (DataHandlerService) applicationContext.getBean(hadlerMap.getHandlerService());
                    ExecutorService executorService = (ExecutorService) applicationContext.getBean(hadlerMap.getTableName() + EXECUTOR_BEAN_SUFFIX);
                    executorService.execute(() -> {
                        dataHandlerService.updateHandle(newData, updateSet, hadlerMap);
                    });
                }
            }
        }
    }

    @Override
    public void insertHandler(JSONObject newData, String tableName) {
        Map<String, List<String>> map = syncDataHandleCenter.getTableTargetSource();
        if (map != null) {
            List<String> sourceList = map.get(tableName);
            if (sourceList != null) {
                for (String temp : sourceList) {
                    ColumnHandleMap hadlerMap = (ColumnHandleMap) applicationContext.getBean(temp + tableName);
                    DataHandlerService dataHandlerService = (DataHandlerService) applicationContext.getBean(hadlerMap.getHandlerService());
                    ExecutorService executorService = (ExecutorService) applicationContext.getBean(hadlerMap.getTableName() + EXECUTOR_BEAN_SUFFIX);
                    executorService.execute(() -> {
                        dataHandlerService.insertHandle(newData, hadlerMap);
                    });
                }
            }
        }
    }

}
